package org.answer.controller;

import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;

import org.answer.constant.MessageConstant;
import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;
import org.answer.entity.Result;
import org.answer.pojo.Permission;
import org.answer.pojo.Role;
import org.answer.service.PermissionService;
import org.answer.service.RoleService;
import org.answer.utils.ErrorMailUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.util.List;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    //log4j日志对象
    private Logger logger = ErrorMailUtils.getLogger();
    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    /**
     * 新增角色
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds){
        try {
            roleService.add(role,permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 查询权限所有信息
     * @return
     */
    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
        try {
            List<Permission> list = permissionService.findAllPermission();
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 根据角色id查询对应的权限id
     * @param id
     * @return
     */
   @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
       try {
           List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
           return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionIds);
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
       }

   }

   @RequestMapping("/edit")
   public Result edit(@RequestBody Role role,Integer[] permissionIds){
       try {
           roleService.edit(role,permissionIds);
           return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
       }
   }
}
